package com.bxait.toymail.order.listener;

import com.bxait.toymail.order.entity.OrderEntity;
import com.bxait.toymail.order.service.OrderService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;

/*
@description:
@ClassName OrderCloseListener
@author Chen
@create 2023-06-18 16:32
@Version 1.0
*/
@Slf4j
@Component
@RabbitListener(queues = "order.release.order.queue")
public class OrderCloseListener {


    @Resource
    OrderService orderService;

    /*
     * @param entity
     * @param channel
     * @param message
     * @==============History===============<br/>
     * @Description //   这个队列的消息就是最终要消费的,到时间后当前这个订单如果还没有付款我们就要进行关单操作
     * @Date 2023/6/18 2023/6/18
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return void
     */
    @RabbitHandler
    public void listener(OrderEntity entity, Channel channel, Message message) throws IOException {
        log.info("收到过期的订单信息：准备关闭订单"+entity.getOrderSn()+"==>"+entity.getId());
        try{
            orderService.closeOrder(entity);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }

    }
}
